package lv.ui;

import lv.domain.User;
import lv.util.CodeUtil;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class RegisterFrame extends JFrame implements MouseListener {
    JButton reset = new JButton();
    JButton register = new JButton();
    JTextField username = new JTextField();
    //JTextField password = new JTextField();
    JPasswordField password = new JPasswordField();
    JPasswordField password2 = new JPasswordField();
    JTextField code = new JTextField();
    //正确的验证码
    JLabel rightCode = new JLabel();
    User user=new User();
    public RegisterFrame() {
        //初始化页面
        initFrame();
        //初始化图片
        initImage();
    }

    private void initImage() {
        //1. 添加用户名文字
        JLabel usernameText = new JLabel(new ImageIcon("image/register/注册用户名.png"));
        usernameText.setBounds(100, 115, 79, 17);
        this.getContentPane().add(usernameText);

        //2.添加用户名输入框
        username.setBounds(205, 114, 200, 30);
        this.getContentPane().add(username);

        //3.添加密码文字
        JLabel passwordText = new JLabel(new ImageIcon("image/register/注册密码.png"));
        passwordText.setBounds(115, 175, 64, 16);
        this.getContentPane().add(passwordText);

        //4.密码输入框
        password.setBounds(205, 170, 200, 30);
        this.getContentPane().add(password);

        //3.添加密码文字
        JLabel passwordText2 = new JLabel(new ImageIcon("image/register/再次输入密码.png"));
        passwordText2.setBounds(85, 225, 96, 17);
        this.getContentPane().add(passwordText2);

        //4.密码输入框
        password2.setBounds(205, 220, 200, 30);
        this.getContentPane().add(password2);
        //验证码提示
        JLabel codeText = new JLabel(new ImageIcon("image/login/验证码.png"));
        codeText.setBounds(133, 266, 50, 30);
        this.getContentPane().add(codeText);

        //验证码的输入框
        code.setBounds(205, 266, 100, 30);
        this.getContentPane().add(code);

        String codeStr = CodeUtil.getCode();
        //设置内容
        rightCode.setText(codeStr);
        //绑定鼠标事件
        rightCode.addMouseListener(this);
        //位置和宽高
        rightCode.setBounds(320, 266, 50, 30);
        //添加到界面
        this.getContentPane().add(rightCode);

        //5.添加登录按钮
        reset.setBounds(115, 320, 128, 47);
        reset.setIcon(new ImageIcon("image/register/重置按钮.png"));
        //去除按钮的边框
        reset.setBorderPainted(false);
        //去除按钮的背景
        reset.setContentAreaFilled(false);
        //给登录按钮绑定鼠标事件
        reset.addMouseListener(this);
        this.getContentPane().add(reset);

        //6.添加注册按钮
        register.setBounds(260, 320, 128, 47);
        register.setIcon(new ImageIcon("image/login/注册按钮.png"));
        //去除按钮的边框
        register.setBorderPainted(false);
        //去除按钮的背景
        register.setContentAreaFilled(false);
        //给注册按钮绑定鼠标事件
        register.addMouseListener(this);
        this.getContentPane().add(register);


        //7.添加背景图片
        JLabel background = new JLabel(new ImageIcon("image/register/background.png"));
        background.setBounds(0, 0, 470, 390);
        this.getContentPane().add(background);
    }
    public void showJDialog(String content) {
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        //给弹框设置大小
        jDialog.setSize(200, 150);
        //让弹框置顶
        jDialog.setAlwaysOnTop(true);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        //弹框不关闭永远无法操作下面的界面
        jDialog.setModal(true);

        //创建Jlabel对象管理文字并添加到弹框当中
        JLabel warning = new JLabel(content);
        warning.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(warning);

        //让弹框展示出来
        jDialog.setVisible(true);
    }
    private void initFrame() {
        //创建一个注册界面
        this.setSize(488, 430);
        //设置标题
        this.setTitle("注册界面");
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(3);
        //显示页面
        this.setVisible(true);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == register) {
            //获取两个文本输入框中的内容
            String usernameInput = username.getText();
            String passwordInput = password.getText();
            String passwordInput2 = password2.getText();
            String codeInput= code.getText();
            if(usernameInput.length()==0 || passwordInput2.length()==0|| passwordInput.length()==0){
                showJDialog("用户名或密码不能为空");
            }else if(codeInput.length()==0)
            {
                showJDialog("验证码不能为空");
            }else if (!codeInput.equalsIgnoreCase(rightCode.getText())) {
                showJDialog("验证码输入错误");
            }else if(!passwordInput.equals(passwordInput2))
            {
                showJDialog("两次输入的密码不一致");
            }
            else {
                if(user.register(usernameInput,passwordInput))
                {
                    showJDialog("注册成功");
                    //关闭当前页面
                    this.setVisible(false);
                    //启动注册界面
                    new LoginFrame();
                }else {
                    showJDialog("用户名已经存在");
                }
            }
        }else if(e.getSource()==reset){


        }
        else if (e.getSource() == rightCode) {
            //获取一个新的验证码
            String code = CodeUtil.getCode();
            rightCode.setText(code);
        }

    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getSource() == reset) {
            reset.setIcon(new ImageIcon("image/register/重置按下.png"));
        } else if (e.getSource() == register) {
            register.setIcon(new ImageIcon("image/register/注册按下.png"));
        }

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.getSource() == reset) {
            reset.setIcon(new ImageIcon("image/register/重置按钮.png"));
        } else if (e.getSource() == register) {
            register.setIcon(new ImageIcon("image/register/注册按钮.png"));
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
